package com.msaon.imageUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args ) throws IOException
    {
    	Integer width = 100;
    	Integer height =100;
    	String dirUrl = null;
    	String type = null;
    	String fileNameContain = null;
    	try {
    		type = args[0];
    		dirUrl = args[1];
    		if(type.toLowerCase().indexOf("r")!=-1){
    			fileNameContain = args[2];
    			del(fileNameContain, dirUrl);
    		}else{
    			width = Integer.valueOf(args[2]);
    			height = Integer.valueOf(args[3]);
    			create(width, height, dirUrl);
    		}
    	} catch (Exception e) {
    		e.printStackTrace();
    		System.out.println(e.getMessage());
    		System.out.println("=====================================================================================");
    		System.out.println("   parm[-c(批量创建缩略图) dirUrl(目标文件夹路径) width(压缩宽度) height(压缩高度)]");
    		System.out.println("   parm[-r(批量删除缩略图) dirUrl(目标文件夹路径) fileNameContain(要删除缩略图的名称规则)");
    		System.out.println("=====================================================================================");
    	}
    }

	private static void create(Integer width, Integer height, String dirUrl)
			throws IOException {
		if(!ImageUtils.isBlank(dirUrl)){
        	List<String> suffixLi = new ArrayList<String>();
        	suffixLi.add("jpeg");
        	suffixLi.add("jpg");
        	suffixLi.add("png");
        	List<File> collector = new ArrayList<File>();//获取所有图片
        	List<String> includes = new ArrayList<String>();
        	ImageUtils.listFiles(new File(dirUrl), collector, includes, ""+width+height,null, suffixLi);
        	System.out.println("=============开始图片生成......共"+(collector==null?0:collector.size()+"张"));
        	if(collector!=null && collector.size()>0){
        		String imgSrc = "";
        		String imgDist = "";
        		for(int i=0;i<collector.size();i++){
        			File img = collector.get(i);
        			System.out.println("============="+img.getAbsolutePath()+"    "+(i+1)+"/"+collector.size());
        			imgSrc = img.getAbsolutePath();
        			int index = imgSrc.lastIndexOf(".");
        			imgDist = img.getAbsolutePath().substring(0, index)
        					+width+height+imgSrc.substring(index, imgSrc.length());
        			ImageUtils.reduceImg(imgSrc, imgDist, width, height, null);
        		}
        	}
        	System.out.println("=============完成=============");
        }
	}
	/**
	 * 
	 * <b> 批量删除指定文件名（模糊）的图片 </b></br>
	 * <em> 说明 </em>
	 * @author Mason  
	 * @param width
	 * @param height
	 * @param dirUrl
	 * @throws IOException
	 * @return void JSON resCode：返回编码(0:查询正常返回;1:参数传异常) data:List数据
	 */
	private static void del(String fileNameContain, String dirUrl)
			throws IOException {
		if(!ImageUtils.isBlank(dirUrl)){
			List<String> suffixLi = new ArrayList<String>();
			suffixLi.add("jpeg");
			suffixLi.add("jpg");
			suffixLi.add("png");
			List<File> collector = new ArrayList<File>();//获取所有图片
			List<String> includes = new ArrayList<String>();
			ImageUtils.listFiles(new File(dirUrl), collector, includes, null, fileNameContain, suffixLi);
			System.out.println("=============开始图片删除......共"+(collector==null?0:collector.size()+"张"));
			if(collector!=null && collector.size()>0){
				for(int i=0;i<collector.size();i++){
					File img = collector.get(i);
					if(img.delete()){
						System.out.println("=============删除"+img.getAbsolutePath()+"    "+(i+1)+"/"+collector.size());
					}else{
						System.out.println("====删除失败"+img.getAbsolutePath()+"    "+(i+1)+"/"+collector.size());
					}
				}
			}
			System.out.println("=============完成=============");
		}
	}
    
}
